﻿Imports MirEvoSvr.Functions

Namespace Drops

    Friend Class DropFile

        Private lstDrops As List(Of Drop)

        Public Sub New(ByVal file As String)

            lstDrops = New List(Of Drop)

            Dim stream As System.IO.StreamReader = FileIO.FileSystem.OpenTextFileReader(file)
            Dim str As String
            Dim tmp As Drop
            Dim lineNo As Integer

            Do Until stream.EndOfStream

                Try

                    str = stream.ReadLine()
                    TrimLineTabs(str)

                    If (Left(str, 1) <> ";") Then

                        tmp = New Drop

                        tmp.ItemName = ReadNextValue(str)
                        tmp.SoMuch = ReadNextValue(str)
                        tmp.InSoMuch = ReadNextValue(str)
                        If str.Length > 0 Then
                            tmp.RepeatChance = ReadNextValue(str)
                        Else
                            tmp.RepeatChance = 1
                        End If

                        tmp.RepeatChance = tmp.RepeatChance * Globals.ServerSettings.Rate_DropMultiplier

                        lstDrops.Add(tmp)

                    End If

                    lineNo += 1

                Catch ex As Exception

                    Throw New Exception("(Line#: " & lineNo & ") - " & ex.Message)

                End Try
            Loop

            stream.Close()

        End Sub

        Public Sub DoDrop(ByRef mob As MirMapMob, ByRef conn As Connection)

            Dim name As String = mob.stats.STR_NAME
            Dim map As MirMap = GetMapFromConn(conn)

            Dim R As Integer, C As Integer

            For Each drop As Drop In lstDrops

                Do Until C >= drop.RepeatChance

                    R = RandNum(1, drop.InSoMuch)
                    If R <= drop.SoMuch Then
                        map.FloorItems.Add(CreateNewItem(drop.ItemName, conn.CharacterID, , ItemLocation.Floor), mob.Pos.INT2_X, mob.Pos.INT2_Y, conn)
                    End If

                    C += 1

                Loop

                C = 0

            Next

        End Sub

    End Class

    Module mDrops

        Public Structure Drop

            Dim ItemName As String
            Dim SoMuch As Int64
            Dim InSoMuch As Int64

            Dim RepeatChance As Integer

        End Structure

        Public Enum DeathType
            Mob = 0
            Player = 1
        End Enum

        Private lstFiles As Dictionary(Of String, DropFile)

        Public Function LoadDrops() As Integer

            Output.AddLine("\B0\F9DR>\F0\B8 Loading drop scripts...")

            'initialize scripts list
            lstFiles = New Dictionary(Of String, DropFile)

            'get collection of script files
            Dim files As Collections.ObjectModel.ReadOnlyCollection(Of String) = FileIO.FileSystem.GetFiles(Globals.sServerPath & "drops\", FileIO.SearchOption.SearchAllSubDirectories, "*.txt")
            Dim tmp As DropFile
            Dim count As Integer, failcount As Integer

            For Each file As String In files

                Try

                    Logger.LoggerWriteLine("DR> Loading '" & FileIO.FileSystem.GetFileInfo(file).Name & "'")

                    tmp = New DropFile(file)

                    Logger.LoggerWriteLine("DR> Parsed OK!")

                    file = Mid(file, InStrRev(file, "\") + 1, file.Length - InStrRev(file, "\") - 4)

                    lstFiles.Add(file, tmp)
                    count += 1


                Catch ex As Exception

                    Output.AddLine("\B0\F9DR>\F0\B8 " & file & " - Drop script generated an error: \F7" & ex.Message & "\F0")
                    failcount += 1

                End Try

            Next

            Output.AddLine("\B0\F9DR>\F2\B8 " & count & "\F0 drop files loaded! (\F7" & failcount & " failed\F0)")

            Return failcount

        End Function

        Public Sub DoDrop(ByRef mob As MirMapMob, ByRef conn As Connection)

            If lstFiles.ContainsKey(mob.stats.STR_NAME) Then

                lstFiles.Item(mob.stats.STR_NAME).DoDrop(mob, conn)

            End If

        End Sub

        Public Sub DoDeathDrops(victimconn As Connection, T As DeathType)

            Dim map As MirMap = GetMapFromConn(victimconn)
            If map Is Nothing Then Exit Sub

            Dim R As Single

            If T = DeathType.Mob Then

                If Globals.ServerSettings.Enable_MobDrops Then

                    If Globals.ServerSettings.Rate_MobBagDrop Then

                        Dim sSql As String = "SELECT a.ID_ITEM FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE BYTE_LOCATION=" & ItemLocation.Inventory & " AND ID_CHARACTER=" & victimconn.CharacterID & ";"
                        Dim row As ArrayList = DB.GameDB.ExecuteQuery(sSql)

                        If Not row Is Nothing Then
                            If row.Count > 0 Then

                                For rowidx As Integer = 0 To row.Count - 1

                                    'generate rnd single between 0.0 and 1.0
                                    R = Rnd()

                                    'if Random seed is below or equal to our probability
                                    If R <= Globals.ServerSettings.Rate_MobBagDrop Then

                                        'drop the item

                                        RemoveItem(row(rowidx)("ID_ITEM"), victimconn)

                                        map.FloorItems.Add(row(rowidx)("ID_ITEM"), victimconn.PositionInfo.INT2_X, victimconn.PositionInfo.INT2_Y, victimconn)

                                    End If

                                Next

                            End If
                        End If

                    End If

                    If Globals.ServerSettings.Enable_MobBodyDrop Then

                        Dim sSql As String = "SELECT a.ID_ITEM FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE " & Globals.strEquipLocationSearch & " AND ID_CHARACTER=" & victimconn.CharacterID & ";"
                        Dim row As ArrayList = DB.GameDB.ExecuteQuery(sSql)

                        If Not row Is Nothing Then
                            If row.Count > 0 Then

                                For rowidx As Integer = 0 To row.Count - 1

                                    'generate rnd single between 0.0 and 1.0
                                    R = Rnd()

                                    'if Random seed is below or equal to our probability
                                    If R <= Globals.ServerSettings.Rate_MobBodyDrop Then

                                        'drop the item

                                        UnEquipItem(row(rowidx)("ID_ITEM"), victimconn, True, True)
                                        map.FloorItems.Add(row(rowidx)("ID_ITEM"), victimconn.PositionInfo.INT2_X, victimconn.PositionInfo.INT2_Y, victimconn)

                                    End If

                                Next

                            End If
                        End If

                    End If

                End If

            ElseIf T = DeathType.Player Then

                If Globals.ServerSettings.Enable_PKDrops Then

                    If Globals.ServerSettings.Rate_PKBagDrop Then

                        Dim sSql As String = "SELECT a.ID_ITEM FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE BYTE_LOCATION=" & ItemLocation.Inventory & " AND ID_CHARACTER=" & victimconn.CharacterID & ";"
                        Dim row As ArrayList = DB.GameDB.ExecuteQuery(sSql)

                        If Not row Is Nothing Then
                            If row.Count > 0 Then

                                For rowidx As Integer = 0 To row.Count - 1

                                    'generate rnd single between 0.0 and 1.0
                                    R = Rnd()

                                    'if Random seed is below or equal to our probability
                                    If R <= Globals.ServerSettings.Rate_PKBagDrop Then

                                        'drop the item

                                        RemoveItem(row(rowidx)("ID_ITEM"), victimconn)

                                        map.FloorItems.Add(row(rowidx)("ID_ITEM"), victimconn.PositionInfo.INT2_X, victimconn.PositionInfo.INT2_Y, victimconn)

                                    End If

                                Next

                            End If
                        End If

                    End If

                    If Globals.ServerSettings.Enable_PKBodyDrop Then

                        Dim sSql As String = "SELECT a.ID_ITEM FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE " & Globals.strEquipLocationSearch & " AND ID_CHARACTER=" & victimconn.CharacterID & ";"
                        Dim row As ArrayList = DB.GameDB.ExecuteQuery(sSql)

                        If Not row Is Nothing Then
                            If row.Count > 0 Then

                                For rowidx As Integer = 0 To row.Count - 1

                                    'generate rnd single between 0.0 and 1.0
                                    R = Rnd()

                                    'if Random seed is below or equal to our probability
                                    If R <= Globals.ServerSettings.Rate_PKBodyDrop Then

                                        'drop the item

                                        UnEquipItem(row(rowidx)("ID_ITEM"), victimconn, True, True)
                                        map.FloorItems.Add(row(rowidx)("ID_ITEM"), victimconn.PositionInfo.INT2_X, victimconn.PositionInfo.INT2_Y, victimconn)

                                    End If

                                Next

                            End If

                        End If

                    End If

                End If

            End If

        End Sub

    End Module

End Namespace